<?php
abstract class Controller
{
    protected $_view;
    protected $_get;
    protected $_post;
    private $_pageContent;

    abstract function showIndex();

    function checkIsLogin()
    {
        if (array_key_exists("credentials", $_SESSION) && $_SESSION['credentials']['userId'] > 0) {
            return true;
        }
        return false;
    }

    function isAdmin()
    {
        return $_SESSION['credentials']['type'] == "admin";
    }

    function getCredentials($key)
    {
        return $_SESSION['credentials'][$key];
    }

    function __construct()
    {
        $this->_get = $_GET;
        $this->_post = $_POST;
    }

    function __get($methodName)
    {
        include_once(ROOT . "/proxy/" . $methodName . ".proxy.php");
        return new $methodName();
    }

    function render($viewName = __CLASS__)
    {
        ob_start();
        $this->_view['get'] = $this->_get;
        $this->_view['post'] = $this->_post;
        $this->_view['config'] = $GLOBALS['config']['view'];
        $this->_view['session'] = $_SESSION;
        $view = $this->_view;
        include_once(ROOT . "/view/" . $viewName . ".view.php");
        $this->_pageContent = ob_get_contents();
        ob_end_clean();
        ob_clean();
        $this->doFilter("render");
    }

    function ajaxRender($content)
    {
        die($content);
    }

    function doFilter($key)
    {
        $filters = $GLOBALS['filter'];
        if (array_key_exists($key, $filters)) {
            foreach ($filters[$key] as $key => $value) {
                include_once(ROOT . "/filter/IFilter.php");
                include_once(ROOT . "/filter/" . $value . ".filter.php");

                $filter = new $value;
                if ($filter instanceof IFilter) {
                    call_user_func(array($filter, "doFilter"), $this->_pageContent);
                }

            }
        }
    }

    function putIntoPage($key, $value)
    {
        $this->_view[$key] = $value;
    }

    function getClientIp()
    {
        if (getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) {
            $onlineip = getenv('HTTP_CLIENT_IP');
        } elseif (getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) {
            $onlineip = getenv('HTTP_X_FORWARDED_FOR');
        } elseif (getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) {
            $onlineip = getenv('REMOTE_ADDR');
        } elseif (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) {
            $onlineip = $_SERVER['REMOTE_ADDR'];
        }
        return $onlineip;
    }

    function checkIdFromGet($id)
    {
        return isset($this->_get[$id]) && $this->_get[$id] > 0;
    }

    function checkIdFromPost($id)
    {
        return isset($this->_post[$id]) && $this->_post[$id] > 0 ? "1" : "0";
    }

    function __call($name, $param)
    {
        if ($this->_get['controller'] == "Admin") {
            include_once(ROOT . "/controller/backend/" . $name . ".controller.php");
        }
        else {
            include_once(ROOT . "/controller/" . $name . ".controller.php");
        }

        $methodName = isset($this->_get['action']) && $this->_get['action'] != '' ? $this->_get['action'] : "showIndex";
        $controllerName = $name . "Controller";
        call_user_func(array(new $controllerName, $methodName));
    }

    function _setcookie($cookiepre, $cookiedomain, $cookiepath, $timestamp, $server, $var, $value, $life = 0, $prefix = 1)
    {
        setcookie(($prefix ? $cookiepre : '') . $var, $value,
                  $life ? $timestamp + $life : 0, $cookiepath,
                  $cookiedomain, $server['SERVER_PORT'] == 443 ? 1 : 0);
    }


}

?>